/*
 * BaseStrategy.h
 *
 *  Created on: 2021��7��16��
 *      Author: root
 */

#ifndef BASESTRATEGY_H_
#define BASESTRATEGY_H_

#include <string>
#include <algorithm>
class DataEngine;
#include "../DataEngine.h"
#include "../Series.cpp"
#include <ctime>
#include <exception>
#include <map>
#include <ctime>
#include <cmath>
#include "../future_tick.h"

using namespace std;

class BaseStrategy
{
private:

    void orderInsert(const string &contractCode, double price,int vol, char flag,int basketId=0);
    ReqOrderMsg temp{};
    bool isBeforeLDate = false;
protected:
	int limitDate=0;

    int errorId=0;
    int expire=INT_MAX;
    short trading_time=0;
	int dealLimit = 5000;
	double minMove = 1.0;
    double multiple=1.0;
    char exchangeId = 0;

	DataEngine *p_engine = nullptr;

	TickDataHf *lastMd = nullptr;

	TickDataHf *preMd = nullptr;
	
    unordered_map<EES_ClientToken ,OrderData> unFinishOrders;



public:
    double sumProfit=0;
    unsigned int dealVol = 0;
	string errorMsg;
    long mdTimes=0;
    json jsonParams;
    json allParams;
	int openSS = 5;
    int tickVol=0;
    vector<position> positions;
	bool isActive = false;
	int strategyId = 0;
	string strategyName;
	int openTimes = 1;
	vector<string> contractCodes;
    string product_id;
	int closeType = 0;

	void preProcessMD(const TickDataHf &md);

	void preProcessOrder(const OrderData &order);

	BaseStrategy();

	explicit BaseStrategy(DataEngine *p_engine);

	void init();

	virtual ~BaseStrategy();

    virtual string getClassName(){};

	virtual void processMD(const TickDataHf &md,const long index){};

	virtual void processOrderCxl(const OrderData &field){};

	void preProcessOrderCxl(const OrderData &field);

	virtual void processDeal(const OrderData &field){};

    virtual void preProOrderDealFinish(const OrderData &field){};

	void preProcessOrderDeal(const OrderData &field);

	void preProcessSymbolStatus(char status, const string &enterTime);

	void preQryProcessSymbolStatus(char status, const string &enterTime);

	virtual void processStatusChanged(const char status, const string &enterTime){};

    void initStrategy(DataEngine *p_engine, const string &params);

	virtual void processSpecialLine(const TickDataHf& md, const SpecialLine& sp){};

	virtual void processMarketOrder(const OrderData &field){};

	virtual void processMarketReject(const OrderData &field){};

	void orderInsertEx(const string &contractCode, double price, int vol, char flag);


	void orderCancel(const OrderData &order);

    virtual void upJsonParams(){};
	void showParams() const;
    virtual json getAllParams() {};
	string getBaseParams() const;

    virtual void saveParams();



    void checkDate();

    void start();

	virtual void postStart(){};

	void stop();
	virtual void postStop(){};

	virtual void processBeginAction(const string &enterTime){};

	virtual void processCallAction(const string &enterTime){};

	virtual void processCallFinishAction(const string &enterTime){};

	virtual void processPauseAction(const string &enterTime){};

	double formatPrice(double price) const;

	bool checkLimitOrder(const string &contractCode, double price,int vol);

	bool checkFakOrder(const string &contractCode, double price, int vol);

	bool isIntraderTime();


    static bool isInCloseTime(const TickDataHf &md);
    static bool isInOpenTime(const TickDataHf &md);

    void preProcessClose(const TickDataHf &md);

    virtual void processClose(const TickDataHf &md){};

    unsigned  int getHangingVol(const string &contractCode, unsigned char EES_SideType);
    unsigned int getSpHangingVol(const string &contractCode);
    unsigned int getBpHangingVol(const string &contractCode);

	int getPreAskPriceCount(const TickDataHf &md, double price) const;

	int getPreBidPriceCount(const TickDataHf &md, double price) const;

	double getPreAskVolPrice(const TickDataHf &md, int count) const;

	double getPreBidVolPrice(const TickDataHf &md, int count) const;

	double getAftAskVolPrice(const TickDataHf &md, int count);

	double getAftBidVolPrice(const TickDataHf &md, int count);

	void savePosition() const;

	void loadPosition();

	string contractToString();

	string getIsRun() const;

    static bool isInBeginTime(const TickDataHf &tick);

    string getPositions() const;

    virtual void loadParams(const string& params){};

    void loadBaseParams();

    void upBaseParams();
};

#endif /* BASESTRATEGY_H_ */
